/**
 *
 */
package com.test.dao.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.test.dao.UserDao;
import com.test.domain.User;

/**
 * @author Administrator
 * @date 2018年5月27日 上午11:05:46
 * 
 */
public class UserDaoImpl implements UserDao {

	/**
	 * 新增方法
	 */
	@Override
	public void add(User user) {
		//1. 加载连接mysql的驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		String sql = "INSERT INTO user(name,password,age) values('"+ user.getName() +
				"','" + user.getPassword() + "',"+ user.getAge() + ")";
		//2. 创建数据库的连接
		Connection conn = null;
		Statement stateMent = null;
		try {
			 conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web01",
					"root", "123456");
			//3.通过连接获取statement
			 stateMent = conn.createStatement();
			//4.用statement 执行sql语句
			stateMent.execute(sql);
			//5.处理查询结果
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				//6.关闭statement
				if(stateMent !=null)stateMent.close();
				//7.关闭连接
				if(conn !=null )conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	
	}

	@Override
	public void mod(User user) {
		
	}

	@Override
	public void del(int id) {
		
	}

	@Override
	public List<User> query(User user) {
		List<User> result = new ArrayList<>();
		//1. 加载连接mysql的驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		String sql = "select * from user";
		//2. 创建数据库的连接
		Connection conn = null;
		Statement stateMent = null;
		try {
			 conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web01",
					"root", "123456");
			//3.通过连接获取statement
			 stateMent = conn.createStatement();
			//4.用statement 执行sql语句
			 ResultSet resultSet = stateMent.executeQuery(sql);
			 
			 ResultSetMetaData resultMetaData =  resultSet.getMetaData();
			 int cols = resultMetaData.getColumnCount();
			//5.处理查询结果
			while(resultSet.next()) {
				Map tempMap  = new HashMap<>();
				for(int i = 1; i <= cols; i++ ) {
//					resultMetaData.getColumnLabel(i);
//					resultSet.getString(i);
//					System.out.println("第"+i +"条记录："+resultMetaData.getColumnLabel(i) +"值："+ resultSet.getString(i));
					tempMap.put(resultMetaData.getColumnLabel(i), resultSet.getString(i));
				}
				User temUser = new User();
//				把map对象转变为User对象
				try {
					BeanUtils.populate(temUser, tempMap);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				//把转换后的User对象添加到返回值的结果集中
				result.add(temUser);
				
				
//				User temUser = new User();
//				 temUser.setId(resultSet.getInt(1));
//				 temUser.setName(resultSet.getString(2));
//				 temUser.setPassword(resultSet.getString(3));
//				 temUser.setAge(resultSet.getInt(4));
//				 
				 
//				 result.add(temUser);
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				//6.关闭statement
				if(stateMent !=null)stateMent.close();
				//7.关闭连接
				if(conn !=null )conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	
		return result;
	}

	@Override
	public int queryByNameAndPassword() {
		return 0;
	}

}
